package cn.edu.neusoft.wuguangsheng.flowerimpression.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.edu.neusoft.wuguangsheng.flowerimpression.R;
import cn.edu.neusoft.wuguangsheng.flowerimpression.activity.CommentGoodsActivity;
import cn.edu.neusoft.wuguangsheng.flowerimpression.activity.OrderInfoActivity;
import cn.edu.neusoft.wuguangsheng.flowerimpression.bean.UserOrderBean;
import cn.edu.neusoft.wuguangsheng.flowerimpression.bean.UserOrderGoodsBean;
import cn.edu.neusoft.wuguangsheng.flowerimpression.service.Service;

public class UserOrderAdapter extends BaseAdapter<UserOrderBean> {
    private List<UserOrderGoodsBean> itemss;
    private LayoutInflater mInflater;
    public UserOrderAdapter(Context context, List<UserOrderBean> items, int layoutResourse) {
        super(context, items, layoutResourse);

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView= LayoutInflater.from(context).inflate(layoutResourse,parent,false);
        UserOrderViewHolder viewHolder = new UserOrderViewHolder(itemView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final UserOrderViewHolder viewHolder=(UserOrderViewHolder) holder;
        final UserOrderBean userOrderBean=items.get(position);
        final String shopname = userOrderBean.getShop_name();
        final String goodnum = userOrderBean.getOrder_goodsnum();
        final String shopid = userOrderBean.getShop_id();
        final double allprice = userOrderBean.getOrder_allprice();
        final String orderid = userOrderBean.getOrder_id();

        viewHolder.userorderitem_shopname.setText(shopname+"------>");
        viewHolder.userorderitem_buyallprice.setText(String.valueOf(allprice));
        viewHolder.userorderitem_num.setText("共"+goodnum+"件商品");

        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(context);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        viewHolder.userorderitem_rv.setLayoutManager(linearLayoutManager);
        itemss=userOrderBean.getOrder_goods();
        CategoryAdapter categoryAdapter = new CategoryAdapter(context,itemss,R.layout.userordergoods_item);
        viewHolder.userorderitem_rv.setAdapter(categoryAdapter);
        viewHolder.userorderitem_rv.setItemAnimator(new DefaultItemAnimator());
        viewHolder.userorderitem_rv.setAdapter(categoryAdapter);
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, OrderInfoActivity.class);
                intent.putExtra("orderid",orderid);
                context.startActivity(intent);

            }
        });
        viewHolder.userorderitem_ckxq.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intentl = new Intent(context, OrderInfoActivity.class);
                intentl.putExtra("orderid",orderid);
                context.startActivity(intentl);
            }
        });
        viewHolder.userorderitem_ljpl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intentl = new Intent(context, CommentGoodsActivity.class);
                intentl.putExtra("order_id",orderid);
                context.startActivity(intentl);
            }
        });
    }

    private class CategoryAdapter extends BaseAdapter<UserOrderGoodsBean> {

        public CategoryAdapter(Context context, List<UserOrderGoodsBean> itemss, int layoutResourse) {
            super(context, itemss, layoutResourse);
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View itemView= LayoutInflater.from(context).inflate(layoutResourse,parent,false);
            ItemViewHolder viewHolder = new ItemViewHolder(itemView);
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            ItemViewHolder viewHolder = (ItemViewHolder) holder;
            final UserOrderGoodsBean userOrderGoodsBean = itemss.get(position);
            final String name = userOrderGoodsBean.getGood_name();
            viewHolder.userordergoods_name.setText(name);
            viewHolder.userordergoods_price.setText("￥"+userOrderGoodsBean.getOg_goodprice());
            viewHolder.userordergoods_num.setText("X"+userOrderGoodsBean.getOg_goodnum());
            String img = Service.BASE_URL+"flower_impression/public/uploads/"+userOrderGoodsBean.getGood_photo();
            Picasso.with(context).load(img).into(viewHolder.userordergoods_img);
//            viewHolder.mTvBookCount.setText(maleBean.getBookCount() + "本");
//            viewHolder.mTvCategoryName.setText(maleBean.getName());
//            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View view) {
//
//                }
//            });
        }



        class ItemViewHolder extends RecyclerView.ViewHolder {
            ImageView userordergoods_img;
            TextView userordergoods_name,userordergoods_price,userordergoods_num;
            public ItemViewHolder(View itemView) {
                super(itemView);
                userordergoods_img = itemView.findViewById(R.id.userordergoods_img);
                userordergoods_name = itemView.findViewById(R.id.userordergoods_name);
                userordergoods_price = itemView.findViewById(R.id.userordergoods_price);
                userordergoods_num = itemView.findViewById(R.id.userordergoods_num);
            }
        }
    }



}
